package com.zwxu.stack;

import java.util.Stack;
/*
 * 1.将A上的n-1个盘子移动到B  a将1给b a将0给c c将0给b 即完成第一步
 * 2.将A上的第n个盘子移动到C   a将2给c
 * 3.将B上n-1个盘子移动到C    b将0给a b将1给c a将0给c
 */
/*
 * 第1com.zwxu.stack.Tower@e026161
       第2com.zwxu.stack.Tower@e026161
     第1com.zwxu.stack.Tower@287b2e39
    第3com.zwxu.stack.Tower@e026161
   第1com.zwxu.stack.Tower@198f1327
   第2com.zwxu.stack.Tower@198f1327
    第4com.zwxu.stack.Tower@e026161
 */
public class HanoiTowerDemo {
	

	public static void main(String[] args) {
		 Tower[] towers = new Tower[3];	
		 for (int i = 0; i < 3; i++) towers[i] = new Tower(i);
		 int n = 4;
		 for (int i = n - 1; i >= 0; i--) towers[0].add(i);
		 towers[0].moveDisks(n, towers[2], towers[1]);
		 Stack<Integer> stack3= towers[2].getDisks();
		 for(int i=0;i<stack3.size();i++){
			 System.out.println(stack3.get(i));
		 }

	}

}

class Tower {
    private Stack<Integer> disks;
	private int i=1;
    // create three towers (i from 0 to 2)
    public Tower(int i) {
        disks = new Stack<Integer>();
    }
	
    // Add a disk into this tower
    public void add(int d) {
        if (!disks.isEmpty() && disks.peek() <= d) {
            System.out.println("Error placing disk " + d);
        } else {
            disks.push(d);
        }
    }
	
    // @param t a tower
    // Move the top disk of this tower to the top of t.
    public void moveTopTo(Tower t) {	    
    		//System.out.println("第"+(i++)+this.toString()  );
    		t.add(disks.pop());
    }
	
    // @param n an integer
    // @param destination a tower
    // @param buffer a tower
    // Move n Disks from this tower to destination by buffer tower
    public void moveDisks(int n, Tower destination, Tower buffer) {
       /* for(int i=0;i<n;i++){
        	this.moveTopTo(buffer);
        	buffer.moveTopTo(destination);
        }*/
    	if (n > 0){
    		//a->c->b
            this.moveDisks(n-1,buffer,destination);
            //此时n=1,a将第n个盘子移动到c
            this.moveTopTo(destination);
            //b->a->c
            buffer.moveDisks(n - 1, destination, this);
        }
    }

    public Stack<Integer> getDisks() {
        return disks;
    }
}